package com.isale.common.utils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.StringTokenizer;

//import org.apache.http.HttpEntity;
//import org.apache.http.ParseException;
//import org.apache.http.client.ClientProtocolException;
//import org.apache.http.client.methods.CloseableHttpResponse;
//import org.apache.http.client.methods.HttpGet;
//import org.apache.http.impl.client.CloseableHttpClient;
//import org.apache.http.impl.client.HttpClients;
//import org.apache.http.util.EntityUtils;


/**
 * Created by wangbing on 2015/9/6.
 */
public class IpUtil {
    private static final Log logger = LogFactory.getLog(IpUtil.class);


    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }

        if (ip != null && ip.indexOf(",") > -1) {
            StringTokenizer stringTokenizer = new StringTokenizer(ip, ",");
            String host = "";
            while (stringTokenizer.hasMoreTokens()) {
                host = stringTokenizer.nextToken();
            }
            if (host != null) {
                host = host.trim();
            }
            return host;
        }

        return ip;
    }

//
//    public static JSONObject loadFromIp(String ip) {
//        //http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=123.123.123.123
//        String url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=" + ip;
//
//        String rst = get(url);
//        logger.debug("url===>" + url);
//        logger.debug("rst===>" + rst);
//
//        if (rst != null) {
//            if (StringUtils.equals(rst, "-2")) {
//                return null;
//            }
//
//            if(StringUtils.equals(rst, "-3")){
//                return null;
//            }
//
//            try {
//                JSONObject jsonObject = JSONObject.fromObject(rst);
//                return jsonObject;
//            } catch (Exception ex) {
//                logger.error("解析定位接口返回 json 格式失败:" + rst, ex);
//                return null;
//            }
//        }
//
//        return null;
//    }
//
//    public static String get(String url) {
//        CloseableHttpClient httpclient = HttpClients.createDefault();
//        try {
//            // 创建httpget.
//            HttpGet httpget = new HttpGet(url);
////            System.out.println("executing request " + httpget.getURI());
//            // 执行get请求.
//            CloseableHttpResponse response = httpclient.execute(httpget);
//            try {
//                // 获取响应实体
//                HttpEntity entity = response.getEntity();
////                System.out.println("--------------------------------------");
//                // 打印响应状态
////                System.out.println(response.getStatusLine());
//                if (entity != null) {
//                    // 打印响应内容长度
////                    System.out.println("Response content length: " + entity.getContentLength());
//                    // 打印响应内容
////                    System.out.println("Response content: " + EntityUtils.toString(entity));
//                    String rst = EntityUtils.toString(entity);
//                    return rst;
//                }
////                System.out.println("------------------------------------");
//
//            } finally {
//                response.close();
//            }
//        } catch (ClientProtocolException e) {
//            e.printStackTrace();
//        } catch (ParseException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        } finally {
//            // 关闭连接,释放资源
//            try {
//                httpclient.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//        return null;
//    }


}
